#![allow(dead_code)]
use matchit::Router;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    params()
}

/// 获取匹配的参数
fn params() -> Result<(), Box<dyn std::error::Error>> {
    let mut m = Router::new();
    m.insert("/users/{id}", ())?;
    m.insert("/user/{*id}", ())?;

    assert_eq!(m.at("/users/1")?.params.get("id"), Some("1"));
    assert_eq!(m.at("/users/23")?.value, &());
    assert_eq!(m.at("/user/1/2/3")?.params.get("id"), Some("1/2/3"));
    assert!(m.at("/users").is_err());

    Ok(())
}

/// 判断路径是否匹配
fn is_match() -> Result<(), Box<dyn std::error::Error>> {
    let mut router = Router::new();
    router.insert("/home", ())?;
    router.insert("/users/{id}", ())?;

    let matched = router.at("/users/978");
    match matched {
        Ok(_) => println!("匹配成功"),
        Err(_) => println!("匹配失败"),
    }
    let matched = router.at("/users1/978");
    match matched {
        Ok(_) => println!("匹配成功"),
        Err(_) => println!("匹配失败"),
    }

    let matched = router.at("/home");
    match matched {
        Ok(_) => println!("匹配成功"),
        Err(_) => println!("匹配失败"),
    }
    
    Ok(())
}
